<template>
  <div>
    <danger-detail-info :form="form"></danger-detail-info>
    <div v-if="form.Status<100">
    <div class="block-box">
      <div class="block-box_item">
        <div class="block-box_title">验收操作</div>
        <eagle-radio-group name="radio" label="判定" required v-model="radio" @change="handleChange">
          <eagle-radio name="1">验收通过</eagle-radio>
          <eagle-radio name="2">验收不通过</eagle-radio>
        </eagle-radio-group>
        <div v-if="radio == 2">
          <eagle-radio-group 
            name="radio"
            label="不通过原因"
            required
            v-model="VerifyNotPassReason"
          >
            <eagle-radio icon-size="18px" name="1"
              >整改措施制定不到位，导致本次验收失败</eagle-radio
            >
            <eagle-radio icon-size="18px" name="2"
              >整改措施恰当，整改落实不到位，导致本次验收失败</eagle-radio
            >
            <eagle-radio icon-size="18px" name="3"
              >不清楚原因，本次验收失败</eagle-radio
            >
          </eagle-radio-group>
        </div>
        <div>
          <eagle-input
            name="uploader"
            label="验收图片"
          >
            <template #input>
              <eagle-image-uploader :images.sync="editForm.VerifyAttachs"></eagle-image-uploader>
            </template>
          </eagle-input>
          <eagle-input
            v-model="editForm.Remarks"
            rows="3"
            :required="required"
            autosize
            label="验收说明"
            type="textarea"
            placeholder="请输入"
            isInputText
          ></eagle-input>
        </div>
      </div>
    </div>
    <div class="btns m-10">
      <eagle-button class="submit-only" @click="submit">提交</eagle-button>
    </div>
    <van-calendar color="#1989fa" v-model="showCalendar" @confirm="onConfirm" />
  </div>
  </div>
</template>

<script>
import { deepClone } from "@/utils";
import { Dialog } from "vant";
import DangerDetailInfo from "./dangerDetailInfo.vue";
export default {
  components: { DangerDetailInfo },
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      id: this.$route.query.id,
      border: false,
      reason: false,
      expenses: false,
      showCalendar: false,
      form: {},
      editForm: {},
      remarks: "",
      images: [],
      radio: "1",
      VerifyNotPassReason: "1",
      required:false,
      isJG:false
    };
  },
  created() {
    this.isJG=this.$store.state.CompanyType=='JG'?true:false
    this.geDetail();
  },
  methods: {
    // 打开日历
    openCalendar(idx) {
      this.idx = idx;
      this.showCalendar = true;
    },
    // 获取详情
    geDetail() {
      let url=this.isJG?"/DangerCheckTaskDetailJG/GetDetail":"/DangerCheckTaskDetail/GetDetail"
      this.$axios
      .get(url,{params:{ id: this.id }})
      .then((res)=>{
        this.form = res.data;
        console.log(this.form.Status)
        this.editForm = deepClone(res.data);
      })
    },
    onConfirm(date) {
      const dateTime = formatDate(date);
      this.editForm[this.idx] = getDate(dateTime);
      this.showCalendar = false;
    },
    submit() {
      if (this.radio == 1) {
        let param = {
          ID: this.editForm.ID,
          Remarks: this.editForm.Remarks,
          VerifyAttachs: this.editForm.VerifyAttachs,
        };
        Dialog.confirm({
          title: "询问",
          message: "隐患一旦验收通过后无法撤销，您确定验收通过吗？",
        })
          .then(() => {
            // on confirm
            let url=this.isJG?'/DangerCheckTaskDetailJG/ConfirmVerify':'/DangerCheckTaskDetail/ConfirmVerify'
            this.$axios
            .post(url,param)
            .then((res)=>{
              this.$toast("保存成功");
              this.geDetail()
              // this.$router.goRefresh(-1);
            })
            .catch((err) => {
              console.log("err", err);
            });
          })
          .catch(() => {
            // on cancel
          });
      } else {
        if (!this.editForm.Remarks) {
          return this.$toast("请填写验收说明");
        }
        let param = {
          ID: this.editForm.ID,
          Remarks: this.editForm.Remarks,
          VerifyAttachs: this.editForm.VerifyAttachs,
          VerifyNotPassReason: this.VerifyNotPassReason,
        };
        let url=this.isJG?"/DangerCheckTaskDetailJG/BackVerify":"/DangerCheckTaskDetail/BackVerify"
        this.$axios
        .post(url,param)
        .then((res)=>{
          this.$toast("保存成功");
          this.$router.goRefresh(-1);
        })
        .catch((err) => {
          console.log("err", err);
        });
      }
    },
    handleChange(val){
      
      this.required = (val =="2")
    }
  },
};
</script>
